package com.zx.dao.mapper.impl;

import com.zx.dao.mapper.Dmp;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.*;
import java.util.Date;

public class DmpImpl extends HashMap<String, Object> implements Dmp {

    @Override
    public Integer getAsInteger(String key) {
        Object value = get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof Integer) {
            return (Integer) value;
        }
        if (value instanceof Number) {
            return ((Number) value).intValue();
        }
        try {
            return Integer.parseInt(value.toString());
        } catch (NumberFormatException e) {
            return null;
        }
    }

    @Override
    public Long getAsLong(String key) {
        Object value = get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof Long) {
            return (Long) value;
        }
        if (value instanceof Number) {
            return ((Number) value).longValue();
        }
        try {
            return Long.parseLong(value.toString());
        } catch (NumberFormatException e) {
            return null;
        }
    }

    @Override
    public String getAsString(String key) {
        Object value = get(key);
        return value != null ? value.toString() : null;
    }

    @Override
    public BigDecimal getAsBigDecimal(String key) {
        Object value = get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof BigDecimal) {
            return (BigDecimal) value;
        }
        if (value instanceof Number) {
            return BigDecimal.valueOf(((Number) value).doubleValue());
        }
        try {
            return new BigDecimal(value.toString());
        } catch (NumberFormatException e) {
            return null;
        }
    }

    @Override
    public Date getAsDate(String key) {
        Object value = get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof Date) {
            return (Date) value;
        }
        if (value instanceof java.sql.Date) {
            return new Date(((java.sql.Date) value).getTime());
        }
        if (value instanceof Timestamp) {
            return new Date(((Timestamp) value).getTime());
        }
        // 可以添加更多日期格式的解析逻辑
        return null;
    }

    @Override
    public List<Object> getAsList(String key) {
        Object value = get(key);
        if (value == null) {
            return Collections.emptyList();
        }
        if (value instanceof List) {
            return (List<Object>) value;
        }
        if (value instanceof Object[]) {
            return Arrays.asList((Object[]) value);
        }
        // 如果值是单个对象，将其包装为单元素列表
        return Collections.singletonList(value);
    }

    @Override
    public Timestamp getAsTimestamp(String key) {
        Object value = get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof Timestamp) {
            return (Timestamp) value;
        }
        if (value instanceof Date) {
            return new Timestamp(((Date) value).getTime());
        }
        if (value instanceof java.sql.Date) {
            return new Timestamp(((java.sql.Date) value).getTime());
        }
        // 可以添加更多时间戳格式的解析逻辑
        return null;
    }

    @Override
    public Boolean getAsBoolean(String key) {
        Object value = get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof Boolean) {
            return (Boolean) value;
        }
        // 处理数字类型：0=false，非0=true
        if (value instanceof Number) {
            return ((Number) value).intValue() != 0;
        }
        // 处理字符串："true"/"1"=true，其他=false
        if (value instanceof String) {
            String str = ((String) value).toLowerCase();
            return "true".equals(str) || "1".equals(str) || "yes".equals(str);
        }
        return null;
    }

    @Override
    public String toXml(String rootName) {
        StringBuilder xml = new StringBuilder();
        xml.append("<").append(rootName).append(">");
        for (Map.Entry<String, Object> entry : entrySet()) {
            xml.append("<").append(entry.getKey()).append(">");
            xml.append(entry.getValue() != null ? entry.getValue().toString() : "");
            xml.append("</").append(entry.getKey()).append(">");
        }
        xml.append("</").append(rootName).append(">");
        return xml.toString();
    }

    @Override
    public String toXml() {
        return toXml("root");
    }

    @Override
    public String toJson() {
        StringBuilder json = new StringBuilder();
        json.append("{");
        Iterator<Map.Entry<String, Object>> iterator = entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            json.append("\"").append(entry.getKey()).append("\":");
            if (entry.getValue() instanceof String) {
                json.append("\"").append(entry.getValue()).append("\"");
            } else {
                json.append(entry.getValue());
            }
            if (iterator.hasNext()) {
                json.append(",");
            }
        }
        json.append("}");
        return json.toString();
    }

    // 可选：添加一些便利方法
    public DmpImpl putValue(String key, Object value) {
        put(key, value);
        return this;
    }
}